package com.example.myapplication1.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.cardview.widget.CardView;

import com.example.myapplication1.R;
import com.example.myapplication1.LakeActivity;
import com.example.myapplication1.LocationDetailActivity;
import com.example.myapplication1.TrackFieldActivity;
import com.example.myapplication1.ClassroomActivity;
import com.example.myapplication1.CafeteriaActivity;
import com.example.myapplication1.GymActivity;

public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                           ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        // 设置图书馆点击事件
        CardView libraryMarkerContainer = view.findViewById(R.id.library_marker_container);
        if (libraryMarkerContainer != null) {
            libraryMarkerContainer.setOnClickListener(v -> {
                Log.d(TAG, "图书馆图标被点击");
                try {
                    Navigation.findNavController(v).navigate(R.id.action_home_to_library);
                } catch (Exception e) {
                    Log.e(TAG, "跳转到图书馆页面时出错", e);
                    Toast.makeText(requireContext(), 
                        "跳转失败: " + e.getMessage(), 
                        Toast.LENGTH_SHORT).show();
                }
            });
        }

        // 设置田径场点击事件
        CardView trackFieldMarker = view.findViewById(R.id.track_field_marker_container);
        if (trackFieldMarker != null) {
            trackFieldMarker.setOnClickListener(v -> {
                Log.d(TAG, "田径场图标被点击");
                try {
                    if (getActivity() == null) {
                        Log.e(TAG, "田径场: getActivity() 返回 null");
                        return;
                    }
                    Intent intent = new Intent(getActivity(), TrackFieldActivity.class);
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e(TAG, "跳转到田径场页面时出错", e);
                    Toast.makeText(requireContext(), 
                        "跳转失败: " + e.getMessage(), 
                        Toast.LENGTH_SHORT).show();
                }
            });
        }

        // 设置人工湖点击事件
        CardView lakeMarker = view.findViewById(R.id.lake_marker_container);
        if (lakeMarker != null) {
            lakeMarker.setOnClickListener(v -> {
                Log.d(TAG, "人工湖图标被点击");
                try {
                    if (getActivity() == null) {
                        Log.e(TAG, "人工湖: getActivity() 返回 null");
                        return;
                    }
                    Intent intent = new Intent(getActivity(), LakeActivity.class);
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e(TAG, "跳转到人工湖页面时出错", e);
                    Toast.makeText(requireContext(), 
                        "跳转失败: " + e.getMessage(), 
                        Toast.LENGTH_SHORT).show();
                }
            });
        }

        // 设置教学楼点击事件
        CardView teachingBuildingMarker = view.findViewById(R.id.teaching_building_marker_container);
        if (teachingBuildingMarker != null) {
            teachingBuildingMarker.setOnClickListener(v -> {
                Log.d(TAG, "教学楼图标被点击");
                try {
                    if (getActivity() == null) {
                        Log.e(TAG, "教学楼: getActivity() 返回 null");
                        return;
                    }
                    Intent intent = new Intent(getActivity(), ClassroomActivity.class);
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e(TAG, "跳转到教学楼页面时出错", e);
                    Toast.makeText(requireContext(), 
                        "跳转失败: " + e.getMessage(), 
                        Toast.LENGTH_SHORT).show();
                }
            });
        }

        // 设置食堂点击事件
        CardView cafeteriaMarker = view.findViewById(R.id.cafeteria_marker_container);
        if (cafeteriaMarker != null) {
            cafeteriaMarker.setOnClickListener(v -> {
                Log.d(TAG, "食堂图标被点击");
                try {
                    if (getActivity() == null) {
                        Log.e(TAG, "食堂: getActivity() 返回 null");
                        return;
                    }
                    Intent intent = new Intent(getActivity(), CafeteriaActivity.class);
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e(TAG, "跳转到食堂页面时出错", e);
                    Toast.makeText(requireContext(), 
                        "跳转失败: " + e.getMessage(), 
                        Toast.LENGTH_SHORT).show();
                }
            });
        }

        // 设置运动馆点击事件
        CardView gymMarker = view.findViewById(R.id.gym_marker_container);
        if (gymMarker != null) {
            gymMarker.setOnClickListener(v -> {
                Log.d(TAG, "运动馆图标被点击");
                try {
                    if (getActivity() == null) {
                        Log.e(TAG, "运动馆: getActivity() 返回 null");
                        return;
                    }
                    Intent intent = new Intent(getActivity(), GymActivity.class);
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e(TAG, "跳转到运动馆页面时出错", e);
                    Toast.makeText(requireContext(), 
                        "跳转失败: " + e.getMessage(), 
                        Toast.LENGTH_SHORT).show();
                }
            });
        }

        return view;
    }

    private void setupLocationMarker(View view, int markerId, String locationName) {
        CardView marker = view.findViewById(markerId);
        if (marker != null) {
            marker.setOnClickListener(v -> {
                Log.d(TAG, locationName + "图标被点击");
                try {
                    if (getActivity() == null) {
                        Log.e(TAG, locationName + ": getActivity() 返回 null");
                        return;
                    }

                    Intent intent = new Intent(getActivity(), LocationDetailActivity.class);
                    intent.putExtra(LocationDetailActivity.EXTRA_LOCATION_NAME, locationName);
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e(TAG, "跳转到" + locationName + "页面时出错", e);
                    Toast.makeText(requireContext(), 
                        "跳转失败: " + e.getMessage(), 
                        Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
} 